﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudFranceTriple.Models
{

    public static class Extension
    {
        private const String _defaultTimeZone = "Romance Standard Time";

        public static TimeZoneInfo GetUserTimeZoneInfo(Int32 UserId)
        {
            String timeZone;
            // User Not Authenticated ? UserId == -1
            if (UserId > 0)
            {
                timeZone = new cloudfrAboq2RsDMEntities().UserProfile.SingleOrDefault(u => u.UserId == UserId).TimeZone;
            }
            else
            {
                timeZone = _defaultTimeZone;
            }
            return TimeZoneInfo.FindSystemTimeZoneById(timeZone);
        }

        /// <summary>
        /// A utiliser sur une liste ou dans une boucle, après avoir appelé "var userTimeZoneInfo = Extension.GetUserTimeZoneInfo(_webSecurity.CurrentUserId);".
        /// </summary>
        /// <param name="utcDateTime"></param>
        /// <param name="timeZoneInfo"></param>
        /// <returns></returns>
        public static DateTime ToUserTimeZone(this DateTime utcDateTime, TimeZoneInfo timeZoneInfo)
        {
            return TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, timeZoneInfo);
        }

        /// <summary>
        /// A utiliser sur un détail.
        /// </summary>
        /// <param name="utcDateTime"></param>
        /// <param name="requesterTimeZone"></param>
        /// <returns></returns>
        public static DateTime ToUserTimeZone(this DateTime utcDateTime, String requesterTimeZone = _defaultTimeZone)
        {
            return TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, TimeZoneInfo.FindSystemTimeZoneById(requesterTimeZone));
        }
    }

    public static class Rules
    {
        private const String _defaultTimeZone = "Romance Standard Time";

        public static DateTime ConvertTimeZoneFromUtcToUser(Int32 UserId, DateTime UtcDateTime)
        {
            String timeZone;

            // User Not Authenticated ? UserId == -1
            if (UserId > 0)
            {
                timeZone = new cloudfrAboq2RsDMEntities().UserProfile.SingleOrDefault(u => u.UserId == UserId).TimeZone;
            }
            else
            {
                timeZone = _defaultTimeZone;
            }
            return TimeZoneInfo.ConvertTimeFromUtc(UtcDateTime, TimeZoneInfo.FindSystemTimeZoneById(timeZone));
        }

        public static DateTime ConvertTimeZoneFromUserToUtc(Int32 UserId, DateTime UtcDateTime)
        {
            String timeZone;

            // User Not Authenticated ? UserId == -1
            if (UserId > 0)
            {
                timeZone = new cloudfrAboq2RsDMEntities().UserProfile.SingleOrDefault(u => u.UserId == UserId).TimeZone;
            }
            else
            {
                timeZone = _defaultTimeZone;
            }
            return TimeZoneInfo.ConvertTimeToUtc(UtcDateTime, TimeZoneInfo.FindSystemTimeZoneById(timeZone));
        }
    }
}
